package com.xhb.ui.tools;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

import net.coobird.thumbnailator.Thumbnails;

public class PNGBitmapConverter {

	public static void converter(String dir) {
		try {
			File dirFile = new File(dir + "/n");

			if (!dirFile.exists()) {
				JOptionPane.showMessageDialog(null, "背景图片不存在");
				return;
			}

			File dirFileHbBmp = new File(dir + "/hbbmp");
			if (!dirFileHbBmp.exists()) {
				dirFileHbBmp.mkdirs();
			}

			for (File file : dirFile.listFiles()) {
				BufferedImage pngImage = ImageIO.read(file);

				// 创建输出流并指定保存位置及名称
				String outputFileName = dirFileHbBmp.getPath() + "/new" + dirFileHbBmp.list().length + ".bmp";
				// 创建一个新的 BMP 图片
				BufferedImage bmpImage = new BufferedImage(pngImage.getWidth(), pngImage.getHeight(),
						BufferedImage.TYPE_INT_RGB);

				// 将 PNG 图片的像素数据复制到 BMP 图片中
				for (int x = 0; x < pngImage.getWidth(); x++) {
					for (int y = 0; y < pngImage.getHeight(); y++) {
						int rgb = pngImage.getRGB(x, y);
						bmpImage.setRGB(x, y, rgb);
					}
				}

				// 将 BMP 图片保存到文件
				ImageIO.write(bmpImage, "bmp", new File(outputFileName));
			}

			JOptionPane.showMessageDialog(null, "生成成功");
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		}

	}
}
